package com.paomi.client.activity.address.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.address.AddAddressActivity;
import com.paomi.client.activity.pay.model.AddressBean;
import com.paomi.client.bean.AddressInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Optional;

public class AddressListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private Typeface mTypeface;
    private List<AddressInfo> data = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public AddressListAdapter(Context context, Typeface typeface) {
        this.context = context;
        this.mTypeface = typeface;
    }

    public void setData(List<AddressInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position) != null && data.get(position).isDefault()) {  //根据你的条件，返回不同的type
            return 0;
        } else {
            return 1;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_defaul_address, parent, false);
            return new DefaultViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_address, parent, false);
            return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (data.size() == 0) {
            return;
        }
        AddressInfo addressInfo = data.get(position);
        if (addressInfo == null) {
            return;
        }
        switch (getItemViewType(position)) {
            case 0:  //不同的布局，做不同的事
                final DefaultViewHolder holderDefault = (DefaultViewHolder) holder;
                holderDefault.tv_default.setVisibility(View.VISIBLE);
                holderDefault.tv_default.setTypeface(mTypeface);
                holderDefault.address_tv.setText(addressInfo.getProvince()+addressInfo.getCity()+addressInfo.getDistrict()+addressInfo.getAddress());
                holderDefault.tv_name.setText(addressInfo.getName());
                holderDefault.tv_phone.setText(addressInfo.getMobile());

                holderDefault.edit_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toChange(addressInfo);
                    }
                });
                longClick(holderDefault.layout, addressInfo.getId() + "", position);

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.setOnItemClick(position);
                        }
                    }
                });
                break;
            case 1:
                final ViewHolder viewHolder = (ViewHolder) holder;
                viewHolder.address_tv.setText(addressInfo.getProvince()+addressInfo.getCity()+addressInfo.getDistrict()+addressInfo.getAddress());
                viewHolder.tv_name.setText(addressInfo.getName());
                viewHolder.tv_phone.setText(addressInfo.getMobile());
                viewHolder.edit_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toChange(addressInfo);
                    }
                });
                longClick(viewHolder.layout, addressInfo.getId() + "", position);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickListener != null) {
                            onItemClickListener.setOnItemClick(position);
                        }
                    }
                });
                break;
        }
    }

    void longClick(LinearLayout holder, String id, int position) {
        holder.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("是否删除");
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (onItemClickListener != null) {
                            onItemClickListener.delete(id, position);
                        }
                    }
                });
                builder.show();
                return false;
            }
        });
    }

    void toChange(AddressInfo addressInfo) {
        Intent i = new Intent(context, AddAddressActivity.class);
        i.putExtra("AddressInfo", addressInfo);
        context.startActivity(i);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.layout)
        LinearLayout layout;
        @BindView(R.id.address_tv)
        TextView address_tv;
        @BindView(R.id.tv_phone)
        TextView tv_phone;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.edit_tv)
        TextView edit_tv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class DefaultViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_default)
        TextView tv_default;//默认地址选中按钮
        @BindView(R.id.address_tv)
        TextView address_tv;
        @BindView(R.id.tv_phone)
        TextView tv_phone;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.edit_tv)
        TextView edit_tv;
        @BindView(R.id.layout)
        LinearLayout layout;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void setOnItemClick(int pos);

        void delete(String id, int pos);
    }
}
